﻿#include<iostream>
#include"Render.h"

using namespace std;
/*************************************************************************************/

void Render::SetupRC()
{
	glEnable(GL_DEPTH_TEST);
	//glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);



     glClearColor(0.0f, 0.15f, 0.0f, 1.0f);
} 

/*************************************************************************************/

void Render::ChangeSize(int width, int height)
{
        SetupRC();
        glViewport(0, 0, width, height);

        glMatrixMode(GL_PROJECTION);

        glLoadIdentity();
        //glOrtho(-width/2,width/2,-height/2,height/2,-1000,1000);//-3.0f,3.0f,-4.0f,4.0f,-1000.0f,1000.0f);
        gluPerspective(60.0f, (float)width/(float)height, 1.0f, 100000.0f);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
}

/*************************************************************************************/

void Render::quit(int code)
{
        SDL_Quit();
        exit(code);
}

/*************************************************************************************/

void Render::InitOpenGLAndSDL(int w, int h, int flags)
{
        //ewentualne zamkniêcie poprzedniego okna(chyba, ¿e to jest pierwsze)
        //zwi¹zane z problemami przy przechodzeniu w tryb pe³noekranowy
        //usuwane s¹ powi¹zania z OpenGLem i nie widaæ obrazu. Dlatego zamykam i tworzê nowe - to przy zmianie rozdzielczoci
        SDL_Quit();

        //pierwsza inicjalizacja obrazu SDL
        if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
        {
                //nie uda³o siê
                fprintf(stderr, "operation failed: %s\n", SDL_GetError());
                quit(1);
        }

        //atrybuty okna
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); 
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
        
        //informacje o obrazie
        info = SDL_GetVideoInfo();      
        if(!info)
        {
                //to nie powinno siê nigdy zdarzyæ ;p
                fprintf(stderr, "video query failed: %s\n", SDL_GetError());
                quit(1);
        }

        //width = info->current_w; - szerokoæ okna
        //height = info->current_h; - wysokoæ okna
        BPP = info->vfmt->BitsPerPixel;
        
        //tworzenie okna
        if(!(screen = SDL_SetVideoMode(w, h, BPP, flags)))
        {
                //np DISPLAY nie zosta³ ustawiony albo niedostêpna rozdzielczoæ
                fprintf(stderr, "video mode set failed: %s\n", SDL_GetError());
                quit(1);
        }
	   
	   SDL_ShowCursor(SDL_DISABLE); //hide cursor
        SDL_WM_SetCaption("Studenciak", NULL);
        SDL_EnableKeyRepeat(1,1);//SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); - domylnie najlepsze ponoæ
                                                //wartoci czyli opónienie przy tworzeniu nowych  zdarzeñ zwi¹zanych z przyciniêtym klawiszem
        //SDL_FillRect(screen, NULL, SDL_MapRGBA(screen->format,0,0,0,0));      
        if(!w || !h)
                ChangeSize(SCREEN_WIDTH, SCREEN_HEIGHT);
        else
                ChangeSize(w, h);
}

/*************************************************************************************/

//Główna funkcja do renderowania sceny
void Render::RenderScene(void)

{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(posx, posy, z, posx, posy, 0.0f, 0.0f, 1.0f, 0.0f);//((float)(screen->h))*sqrt(3.0f)/2.0f(screen->h)*sqrt(3.0f)/2.0f, posx, posy, 0.0f, 0.0f, 1.0f, 0.0f);//((float)(screen->h))*sqrt(3.0f)/2.0f
	
	//PODŁOGA
	//Floor floor;
	floor.renderFloor();

	//PUNKT
	//zmiana z punktu na czerwone koło
	glPushMatrix();
	glTranslatef(posx, posy, 0.02f); //przesuń do aktualnej pozycji układ współrzędnych
	glBegin(GL_POLYGON);
	{
		glColor3f(1.0f, 0.0f, 0.0f);
		//GLfloat const PI = 3.14f;
		GLfloat x,y;
			
		//100 punktów na okręgu
		for(int i = 1; i <= 100; i++)
		{
			x = this->r*cos(i*(2*PI/100.0f));
			y = this->r*sin(i*(2*PI/100.0f));
			glVertex3f(x,y,0.05f);
		}
	}	
	glEnd();
	glPopMatrix();
	

	//BUDYNKI
	cubes.RenderCubes();//allcubes[0].RenderCube();//60.0f,-400.0f, 200.0f, 300.0f, 140.0f, 300.0f);	
	//KAMERY
	cameras.ProceedCameras();

	world.Bounds();
	
	SDL_GL_SwapBuffers();
}

/*************************************************************************************/

void Render::Keypress(SDL_keysym* keysymptr)
{
        int w = screen->w, h = screen->h; //zapisywanie aktualnych ustawieñ okna
        int flags = screen->flags;          //w razie gdyby nie uda³a siê zmiana rozdzielczoci
        keystate = SDL_GetKeyState(NULL);

        switch(keysymptr->sym)
        {
        case 27:
                exit(0);
                break;

        //zmiana rozdzielczoci
        case 'r':
        case 'R':
                switch(info->current_h)
                {
                case 480:
                        InitOpenGLAndSDL(800, 600);
                        break;
                case 600:
                        InitOpenGLAndSDL(1024, 768);
                        break;
                case 768:
                        InitOpenGLAndSDL(1280, 800);
                        break;
                default:
                        InitOpenGLAndSDL(640, 480);
                        break;
                }
                
                if(screen == NULL) screen = SDL_SetVideoMode(w, h, BPP, flags);
                if(screen == NULL) isrunning = false; 
                break;
        case 's':
        case 'S':
                break;

        //fullscreen
        case 'f':
        case 'F':
                InitOpenGLAndSDL(0, 0, SDL_OPENGL | SDL_SWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
                if(screen == NULL) screen = SDL_SetVideoMode(w, h, BPP, flags);
                if(screen == NULL) isrunning = false; 

                break;
                

        case SDLK_LEFT:
        case SDLK_RIGHT:
        case SDLK_UP:
        case SDLK_DOWN:
                if(keystate[SDLK_LEFT]) posx-=this->speed;
                if(keystate[SDLK_RIGHT]) posx+=this->speed;
                if(keystate[SDLK_DOWN]) posy-=this->speed;
                if(keystate[SDLK_UP]) posy+=this->speed;
                
                break;
        case '=':
			//if(z>30)
                z-=300;
                break;
        case '-':
			//if(z<3000)
                z+=300;
                break;
        }

        RenderScene();

}

/*************************************************************************************/

void Render::Keyrelease(SDL_keysym *keysymptr)
{

}

/*************************************************************************************/